package com.cw.jeeyt.controller.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.common.DataTable;
import com.cw.jeeyt.common.ResultData;
import com.cw.jeeyt.common.enums.EnumOperateFlag;
import com.cw.jeeyt.common.web.BaseController;
import com.cw.jeeyt.service.bo.api.ApiFacadeObjEnumBo;
import com.cw.jeeyt.service.intf.api.ApiFacadeObjEnumService;
import com.cw.jeeyt.service.directive.PageDictDirective;
import com.cw.jeeyt.service.directive.PermissionDirective;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 枚举对象信息表控制层
 *
  * @author chenw
 * @version 1.0.0
 */
@Controller
@RequestMapping("/apiFacadeObjEnum")
public class ApiFacadeObjEnumController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(ApiFacadeObjEnumController.class);
    @Resource
    private ApiFacadeObjEnumService apiFacadeObjEnumService;

    /**
     * 跳转到枚举对象信息表列表查询页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/query")
    public ModelAndView query() throws Exception {

        ModelAndView mv = new ModelAndView("api/apiFacadeObjEnum_index");
        mv.addObject("permission", new PermissionDirective());
        mv.addObject("PageDict", new PageDictDirective());
        return mv;
    }

    /**
     * 加载DataTable列表数据
     *
     * @param dataTable 请求数据
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/listApiFacadeObjEnum")
    public ResultData listApiFacadeObjEnum(@RequestBody DataTable dataTable) throws Exception {

        ApiFacadeObjEnumBo apiFacadeObjEnumBo = JSONObject.parseObject(JSON.toJSONString(dataTable.getQueryCond()),
            ApiFacadeObjEnumBo.class);
        if (apiFacadeObjEnumBo == null) {
            apiFacadeObjEnumBo = new ApiFacadeObjEnumBo();
        }
        PageInfo<ApiFacadeObjEnumBo> pageInfo = apiFacadeObjEnumService.queryByPage(apiFacadeObjEnumBo,
            dataTable.getStart(), dataTable.getLength(), dataTable.getOrder()[0]);
        return ResultData.builder()
            .draw(dataTable.getDraw())
            .data(CollectionUtil.isNotEmpty(pageInfo.getList())?pageInfo.getList():new ArrayList())
            .recordsTotal(pageInfo.getTotal())
            .recordsFiltered(pageInfo.getTotal())
            .build();
    }

    /**
     * 跳转到选择枚举对象信息表列表查询页面
     *
     * @param selectType
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/query_select")
    public ModelAndView querySelect(String selectType, String returnMethod) {

        ModelAndView mv = new ModelAndView("api/apiFacadeObjEnum_selectIndex");
        mv.addObject("permission", new PermissionDirective());
        mv.addObject("PageDict", new PageDictDirective());
        mv.addObject("selectType", selectType);
        mv.addObject("returnMethod", returnMethod);
        return mv;
    }

    /**
     * 跳转到枚举对象信息表表单页面
     *
     * @param operation 操作  add 新增 edit修改 view查看
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-form")
    public ModelAndView editForm(Long id, String operation) throws Exception {

        ModelAndView mv = new ModelAndView("api/apiFacadeObjEnum_form");
        ApiFacadeObjEnumBo apiFacadeObjEnumBo = null;
        List<ApiFacadeObjEnumBo> itemEnumList = new ArrayList<ApiFacadeObjEnumBo>();
        if (EnumOperateFlag.ADD.getCode()
            .equals(operation)) {
            apiFacadeObjEnumBo = new ApiFacadeObjEnumBo();
        } else {
            apiFacadeObjEnumBo = apiFacadeObjEnumService.getApiFacadeObjEnumById(id);
            itemEnumList = apiFacadeObjEnumService.queryListByParentId(apiFacadeObjEnumBo.getId());
        }
        mv.addObject("apiFacadeObjEnum", apiFacadeObjEnumBo);
        mv.addObject("itemEnumList", itemEnumList);
        mv.addObject("operation", operation);
        mv.addObject("PageDict", new PageDictDirective());
        mv.addObject("permission", new PermissionDirective());
        return mv;
    }

    /**
     * 跳转到枚举对象信息表表单页面
     *
     * @param operation 操作  add 新增 edit修改 view查看
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-itemForm")
    public ModelAndView toItemForm(Long id, Long parentId, String operation)
    throws Exception {

        ModelAndView mv = new ModelAndView("api/apiFacadeObjEnum_itemForm");
        ApiFacadeObjEnumBo apiFacadeObjEnumBo = null;
        if (EnumOperateFlag.ADD.getCode()
            .equals(operation)) {
            apiFacadeObjEnumBo = new ApiFacadeObjEnumBo();
            apiFacadeObjEnumBo.setParentId(parentId);
        } else {
            apiFacadeObjEnumBo = apiFacadeObjEnumService.getApiFacadeObjEnumById(id);
        }
        mv.addObject("apiFacadeObjEnum", apiFacadeObjEnumBo);
        mv.addObject("operation", operation);
        mv.addObject("permission", new PermissionDirective());
        return mv;
    }

    /**
     * 根据操作类型保存枚举对象信息表数据
     *
     * @param apiFacadeObjEnumBo
     * @param operation add 新增  edit 修改
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/saveApiFacadeObjEnum", method = RequestMethod.POST)
    public JsonResultDto saveApiFacadeObjEnum(ApiFacadeObjEnumBo apiFacadeObjEnumBo,
        String operation) {

        Long id = null;
        JsonResultDto jsonResultDto = new JsonResultDto(false);
        Map<String, Object> map = new HashMap<>();
        try {
            if (operation.equals(EnumOperateFlag.ADD.getCode())) {
                id = apiFacadeObjEnumService.insert(apiFacadeObjEnumBo);
            } else if (operation.equals(EnumOperateFlag.EDIT.getCode())) {
                apiFacadeObjEnumService.update(apiFacadeObjEnumBo);
                id = apiFacadeObjEnumBo.getId();
            }
            jsonResultDto.setMsg("保存成功");
            jsonResultDto.setSuccess(true);
            map.put("id", id);
            jsonResultDto.setResultData(map);
        } catch (Exception e) {
            jsonResultDto.setMsg("保存失败:" + e.getMessage());

            e.printStackTrace();
            logger.error("保存枚举对象信息表失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

    /**
     * 删除枚举对象信息表
     *
     * @param apiFacadeObjEnumBo
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public JsonResultDto delete(ApiFacadeObjEnumBo apiFacadeObjEnumBo) throws Exception {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            apiFacadeObjEnumService.delete(apiFacadeObjEnumBo);
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();

            jsonResultDto.setMsg("删除失败：" + e.getMessage());
            logger.error("删除枚举对象信息表失败：" + e.getMessage());
        }
        return jsonResultDto;
    }
}
